﻿using System;

namespace SapaFinance.Domain.Model
{
    public class Account : AbstractEntity
    {
        public virtual bool IsPrimary { get; private set; }
        public virtual bool IsHidden { get; private set; }
        public virtual Identity ChangedBy{ get; private set; }
        public virtual Identity CreatedBy { get; private set; }
        public virtual Identity OwnedBy { get; private set; }

        protected Account() { }

        public Account(string number, Currency currency, Bank bank, Corporation corporation, User user)
            : this()
        {
            Bank = bank;
            Corporation = corporation;
            Number = number;
            Currency = currency;
            CreatedBy = new Identity(user);
            Change(user);
        }
      
        public virtual Corporation Corporation { get; private set; }
        public virtual Bank Bank { get; private set; }
        public virtual string Number { get; private set; }
        public virtual Currency Currency { get; private set; }
        public virtual string Iban { get; set; }

       

        public void Change(User user)
        {
            ChangedBy=new Identity(user);
        }
      /// <summary>
      /// если скрываем счет, то делаем не основным
      /// </summary>
      /// <param name="bit"></param>
        public void Delete(bool bit)
        {
            IsHidden = bit;
            if (IsHidden)
            {
                SetPrimary(false);
            }
        }
        /// <summary>
        /// Если устанавливаем как основной, то возвращаем из скрытых счетов
        /// </summary>
        /// <param name="bit"></param>
        public void SetPrimary(bool bit)
        {
            IsPrimary = bit;
            if (IsPrimary)
            {
                Delete(false);
            }
        }


        public void SetOwner(User user)
        {
            OwnedBy = new Identity(user);
        }
        public override bool Equals(object obj)
        {
            var compare = (Account)obj;
            if (ReferenceEquals(null, compare)) return false;

            if (Bank != null && Corporation != null && Currency != null)
            {
                return Bank.Equals(compare.Bank) && Corporation.Equals(compare.Corporation) &&
                       Number.Equals(compare.Number) && Currency.Equals(compare.Currency);
            }
            return UID != Guid.Empty ? base.Equals(compare) : false;
        }

    }
}